package com.yequan.thread.threadpool.mythreadpoolmanager;


/**
 * @author yequan
 * @date 2019/12/15
 */
public class Test {

    public static void main(String[] args) throws Exception {
        CustomThreadPoolManager ioIntensiveThreadPoolManager = ThreadPoolManagerFactory.newIoIntensiveThreadPoolManager();
        testCommon(ioIntensiveThreadPoolManager);
    }

    /**
     * 测试： 提交15个执行时间需要3秒的任务,看线程池的状况
     *
     * @param threadPoolExecutor 传入不同的线程池，看不同的结果
     * @throws Exception
     */
    public static void testCommon(CustomThreadPoolManager threadPoolExecutor) throws Exception {
        // 测试： 提交15个执行时间需要3秒的任务，看超过大小的2个，对应的处理情况
        for (int i = 0; i < 15; i++) {
            int n = i;
            threadPoolExecutor.executeTask(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("开始执行：" + n);
                        Thread.sleep(3000L);
                        System.err.println("执行结束:" + n);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            System.out.println("任务提交成功 :" + i);
        }
        // 查看线程数量，查看队列等待数量
        Thread.sleep(500L);
        System.out.println("当前线程池线程数量为：" + threadPoolExecutor.getActiveCount());
        System.out.println("当前线程池等待的数量为：" + threadPoolExecutor.getTaskCount());
        // 等待15秒，查看线程数量和队列数量（理论上，会被超出核心线程数量的线程自动销毁）
        Thread.sleep(15000L);
        System.out.println("当前线程池线程数量为：" + threadPoolExecutor.getActiveCount());
        System.out.println("当前线程池等待的数量为：" + threadPoolExecutor.getTaskCount());
    }

}
